<?php

namespace App\Customer\Controller;

use App\Customer\Model\CaseModel;
use App\Service\Dict;
use App\Service\DictConstant;
use EchoPHP\Http\Request;

class CaseList extends Controller {

    /**
     * 获取职位下的流程简历总数
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getCaseFlowatCount(Request $request) {
        $caseId = $request->getParam('caseid/d', 0);
        $subId = $request->getParam('subid/d', 0);

        $caseModel = new CaseModel();
        //校验subid和caseid是否属于该用户
        $hrUid = $this->getEhireHruid();
        $subIds = $this->getSubIds();
        //判断用户是否和subid有关联
        $checkResult = $caseModel->checkSubIdByHrUid($subIds, $subId);
        if ($caseId <= 0 || $subId <= 0 || !$checkResult) {
            return $this->json([
                'status' => false,
                'message' => '参数出錯！'
            ]);
        }
        //组装数据查询es
        $searchParams = [
            'subid' => $subId,
            'caseid' => $caseId,
            'status' => '01'
        ];

        $group = ['flowat'];
        $countData = $caseModel->getFlowatCount($searchParams, $group);
        return $this->json([
            'status' => true,
            'message' => '',
            'flowat' => $countData
        ]);

    }


    /**
     * 获取职位列表
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getCaseList(Request $request) {
        $subId = $request->getParam('subid/d', 0);
        $caseName = $request->getParam('casename', '');
        $pageNum = $request->getParam('pagenum/d', 1);
        $pageLimit = $request->getParam('pagelimit/d', 10);
        $caseId = $request->getParam('caseid/d', 0);
        $caseModel = new CaseModel();
        $hrUid = $this->getEhireHruid();
        $subIds = $this->getSubIds();
        //判断用户是否和subid有关联
        if (empty($subId) || !$caseModel->checkSubIdByHrUid($subIds, $subId)) {
            return $this->json([
                'status' => false,
                'message' => '参数出錯！'
            ]);
        }

        //通过redis获取职位id
        if ($caseId <= 0) {
            $caseIds = $caseModel->getRedisCaseIds($hrUid, $subId);
            if (empty($caseIds)) {
                return $this->json([
                    'status' => true,
                    'message' => '',
                    'caseinfo' => [],
                    'totalcount' => 0
                ]);
            }
        } else {
            $caseIds = [$caseId];
        }

        //根据caseid 获取职位列表
        $caseIdsImplode = implode(',', $caseIds);
        list($pageNum, $rowFrom, $rowTo) = $caseModel->paginationParameters($pageNum, $pageLimit);
        $param = [
            'casename' => $caseName,
            'status' => "01"
        ];
        //获取职位列表
        $field = 'caseid,SCase.casename,SCase.caseshowname,SCase.area,SCase.createdate,SCase.status';
        $caseList = $caseModel->getCaseList($field, $caseIdsImplode, $rowFrom, $rowTo, 'caseid desc', $param);
        if (empty($caseList)) {
            return $this->json([
                'status' => true,
                'message' => '',
                'caseinfo' => [],
                'totalcount' => 0
            ]);
        }

        //获取负责人
        $searchParam = [
            'caseids' => $caseIds,
            'ischarger' => 1
        ];

        $adviserData = $caseModel->getCaseContacts($searchParam, 'b.caseid,u.cname');
        $advisers = [];
        if (!empty($adviserData)) {
            foreach ($adviserData as $adviser) {
                $advisers[$adviser['caseid']][] = $adviser['cname'];
            }
            unset($adviserData);
        }

        $totalCountData = $caseModel->getCaseList('count(caseid)  as total ', $caseIdsImplode, 0, 1000, 'caseid desc', $param);
        $totalCount = isset($totalCountData[0]['total']) ? $totalCountData[0]['total'] : 0;
        $attributes = [
            'area' => [DictConstant::DD_CITY_COUNTY, '']
        ];

        $dict = new Dict();
        $dict->rowTrans($caseList, $attributes, true, ',', ',');

        //获取进展天数
        $now = time();
        foreach ($caseList as &$value) {
            $value['indays'] = round(($now - strtotime($value['createdate'])) / 3600 / 24);
            $value['advisername'] = isset($advisers[$value['caseid']]) && !empty($advisers[$value['caseid']]) ? implode(',', $advisers[$value['caseid']]) : '';
            $value['casestatus'] = $caseModel->getCaseStatus($value['status']);
        }

        return $this->json([
            'status' => true,
            'message' => '',
            'caseinfo' => $caseList,
            'totalcount' => $totalCount
        ]);
    }

    /**
     * 首页统计
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getCaseIndexCount(Request $request) {
        $subId = $request->getParam('subid/d', 0);
        $caseModel = new CaseModel();
        $hrUid = $this->getEhireHruid();
        $subIds = $this->getSubIds();
        //判断用户是否和subid有关联
        if (empty($subId) || !$caseModel->checkSubIdByHrUid($subIds, $subId)) {
            return $this->json([
                'status' => false,
                'message' => 'subid出错！'
            ]);
        }

        $caseCount = $caseModel->getCaseCount($hrUid, $subId);
        return $this->json([
            'status' => true,
            'message' => '',
            'casecount' => $caseCount,
        ]);

    }

    /**
     * 获取职位联系人
     * @param Request $request
     */
    public function getCaseContacts(Request $request) {
        $subId = $request->getParam('subid/d', 0);
        $caseModel = new CaseModel();
        $hrUid = $this->getEhireHruid();
        $subIds = $this->getSubIds();
        //判断用户是否和subid有关联
        if (empty($subId) || !$caseModel->checkSubIdByHrUid($subIds, $subId)) {
            return $this->json([
                'status' => false,
                'message' => 'subid出错！'
            ]);
        }

        $caseIds = $caseModel->getRedisCaseIds($hrUid, $subId);

        if (empty($caseIds)) {
            return $this->json([
                'status' => true,
                'message' => '',
                'contactsList' => [],
            ]);
        }

        $searchParam = [
            'caseids' => $caseIds,
            'ischarger' => 1
        ];

        $contacts = $caseModel->getCaseContacts($searchParam, 'u.cname,u.mobilephone', ' a.oprid');

        return $this->json([
            'status' => true,
            'message' => '',
            'contactsList' => $contacts,
        ]);
    }

}